package com.project.light.service.impl;

import com.project.light.dto.DataStationInfo;
import com.project.light.dto.DataStationInfoParam;
import com.project.light.dto.DateParam;
import com.project.light.entity.GroupStation;
import com.project.light.entity.StationInfo;
import com.project.light.entity.StationInfoR;
import com.project.light.mapper.GroupMapper;
import com.project.light.mapper.GroupStationMapper;
import com.project.light.mapper.StationInfoMapper;
import com.project.light.mapper.StationInfoRMapper;
import com.project.light.service.StationInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StationInfoServiceImpl implements StationInfoService {

    @Resource
    private StationInfoMapper stationInfoMapper;
    @Resource
    private StationInfoRMapper stationInfoRMapper;
    @Resource
    private GroupStationMapper groupStationMapper;
    @Resource
    private GroupMapper groupMapper;

    @Override
    public StationInfo getById(Integer id) {
        StationInfo stationInfo = stationInfoMapper.selectByPrimaryKey(id);
        return stationInfo;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(StationInfo entity) throws Exception {
        stationInfoMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(StationInfo entity) throws Exception {
        stationInfoMapper.updateByPrimaryKey(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        stationInfoMapper.deleteByPrimaryKey(id);
    }


    @Override
    public StationInfoR getByIdR(Integer id, String sysId) {
        Example example = new Example(StationInfoR.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("stationId", id);
        c.andEqualTo("sysId", sysId);
        List<StationInfoR> list = stationInfoRMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertR(StationInfoR entity) throws Exception {
        stationInfoRMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateR(StationInfoR entity) throws Exception {
        stationInfoRMapper.updateByPrimaryKey(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteR(Integer id) throws Exception {
        stationInfoRMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GroupStation getGroupByStationId(Integer stationId) {
        Example example = new Example(GroupStation.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("stationID", stationId);
        List<GroupStation> list = groupStationMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertGroupStation(GroupStation entity) throws Exception {
        groupStationMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateGroupStation(GroupStation entity) throws Exception {
        groupStationMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteGroupStation(Integer id) throws Exception {
        groupStationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<DataStationInfoParam> getSysList(DateParam dateParam) {
        List<DataStationInfoParam> list = stationInfoMapper.getSysList(dateParam);
        return list;
    }

    @Override
    public List<DataStationInfo> getStationList(DateParam dateParam) {
        List<DataStationInfo> list = stationInfoMapper.getStationList(dateParam);
        return list;
    }

    @Override
    public List<DataStationInfo> getIdList(DateParam dateParam) {
        List<DataStationInfo> list = stationInfoMapper.getIdList(dateParam);
        return list;
    }
}
